/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servergui;

import DataObject.MyData;
import DataObject.MyRowData;
import DataObject.TableObject;
import Utility.ColorPane;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Vector;
import javax.swing.*;

/**
 *
 * @author Backkom
 */
public class QueryView extends JPanel {

    protected JTable tables;
    public String databaseName;
    protected ServerScreen frameParent;
    protected Vector<TableObject> listObject;
    protected String[] tbHeader;
    protected JTextArea report;
    protected CardLayout cards;
    protected JPanel belowPane;
    protected JPanel labels;
    JFileChooser fc = new JFileChooser();
    ColorPane Output;

    public QueryView(ServerScreen parent, String dbName) {
        databaseName = dbName;
        listObject = new Vector<TableObject>();
        setDoubleBuffered(true);
        BorderLayout layout = new BorderLayout();
        this.setLayout(layout);

        frameParent = parent;
        Output = new ColorPane();
        Output.addKeyListener(new java.awt.event.KeyAdapter() {

            public void keyPressed(java.awt.event.KeyEvent evt) {
                Output.EditorQueryKeyPressed(evt);
            }
        });
        JScrollPane scroll = new JScrollPane(Output);
        labels = new JPanel();
        JLabel runLabel = new javax.swing.JLabel();
        runLabel.setToolTipText("Excute query");
        runLabel.setIcon(new javax.swing.ImageIcon("src/run.png"));

        runLabel.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Excute();
            }
        });
        labels = new JPanel();
        JLabel clrLabel = new javax.swing.JLabel();
        clrLabel.setToolTipText("Clear text");
        clrLabel.setIcon(new javax.swing.ImageIcon("src/cancel.png"));

        clrLabel.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ClearText();
            }
        });

        labels.add(runLabel);
        labels.add(clrLabel);

        report = new JTextArea();
        report.setEditable(false);
        report.setForeground(Color.red);
        report.setFont(new Font("Arial", Font.BOLD, 16));
        JScrollPane sc = new JScrollPane(report);
        cards = new CardLayout();
        belowPane = new JPanel(cards, true);
        belowPane.setPreferredSize(new Dimension(this.getWidth(), 200));
        this.add(belowPane, BorderLayout.SOUTH);
        this.add(labels, BorderLayout.NORTH);
        this.add(scroll, BorderLayout.CENTER);
        belowPane.add("report", sc);
        tables = new JTable();
        InitTable();
    }

    private void InitTable() {
        tables.setDoubleBuffered(true);
        tables.setAutoCreateRowSorter(true);
        tables.setCellSelectionEnabled(false);
        tables.setCellEditor(new DefaultCellEditor(new JTextField()));
        JScrollPane scroll = new JScrollPane(tables);
        belowPane.add("table", scroll);
        this.paintAll(tables.getGraphics());
        cards.show(belowPane, "report");
    }

    public void UpdateTable() {
        TableValues values = new TableValues(listObject, tbHeader);
        values.isEditCell = false;
        tables.setModel(values);
        tables.paintAll(tables.getGraphics());
    }

    public void ClearText() {
        Output.setText("");
        report.setText("");
    }

    public void Excute() {
        String query = Output.getText();
        if (report.getText().length() > 60) {
            report.setText("");
            report.paintAll(report.getGraphics());
        }
        Vector<TableObject> res = new Vector<TableObject>();
        String result = frameParent.Process(databaseName, query, res);
        //MySentence sentence = 
        if (res != null && !res.isEmpty()) //Select has result
        {
            Vector<TableObject> data = Parse2TableObject(res);
            listObject.clear();
            listObject.addAll(data);
            UpdateTable();
            cards.show(belowPane, "table");
            report.setText("");
            belowPane.paintAll(belowPane.getGraphics());
        } else {
            report.append(result + "\n");
            cards.show(belowPane, "report");
        }
    }

    private Vector<TableObject> Parse2TableObject(Vector res) {
        Vector<TableObject> tbO = new Vector<>();
        int size = ((MyRowData) res.get(0)).Datas.size();
        tbHeader = new String[size];
        for (int i = 0; i < res.size(); i++) {
            Vector<MyData> dt = (Vector<MyData>) ((MyRowData) res.get(i)).Datas;
            Object[] o = new Object[size];
            for (int j = 0; j < size; j++) {
                if (i == 0) {
                    tbHeader[j] = dt.get(j).Column;
                }
                o[j] = dt.get(j).Value;
            }
            tbO.add(new TableObject(o));
        }
        return tbO;
    }
}
